package com.zw.admin.server.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zw.admin.server.page.table.PageTableRequest;
import com.zw.admin.server.page.table.PageTableHandler;
import com.zw.admin.server.page.table.PageTableResponse;
import com.zw.admin.server.page.table.PageTableHandler.CountHandler;
import com.zw.admin.server.page.table.PageTableHandler.ListHandler;
import com.zw.admin.server.utils.DateUtils2;
import com.zw.admin.server.utils.HttpServletUtil;
import com.alibaba.fastjson.JSONObject;
import com.zw.admin.server.component.PurchOrderComponent;
import com.zw.admin.server.dao.PPurchOrdersDao;
import com.zw.admin.server.model.PPurchOrders;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/pPurchOrderss")
public class PPurchOrdersController {

    @Autowired
    private PPurchOrdersDao pPurchOrdersDao;


    @Autowired
    private  PurchOrderComponent purchOrderComponent ;
    
    @PostMapping
    @ApiOperation(value = "保存")
    public PPurchOrders save(HttpServletRequest request ) {

    	JSONObject  requestJo= HttpServletUtil.parameterMap2Json(request);
    
    	//@RequestBody 
    	PPurchOrders pPurchOrders = new PPurchOrders();
    	pPurchOrders.setSupplierNo(requestJo.getInteger("supplierNo"));

    	pPurchOrders.setType(requestJo.getInteger("type"));
    	pPurchOrders.setTransitType(requestJo.getInteger("transitType"));
    	pPurchOrders.setPlanReceiveDay( DateUtils2.parse(requestJo.getString("planReceiveDay"),DateUtils2.FORMAT_SHORT ) );
    	pPurchOrders.setBuydate( new Date());
        pPurchOrdersDao.insert(pPurchOrders);

        return pPurchOrders;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取")
    public PPurchOrders get(@PathVariable Long id) {
    	PPurchOrders pPurchOrders= pPurchOrdersDao.getById(id);
    	purchOrderComponent.fixPurchOrderStatusTitle(pPurchOrders);
        return pPurchOrders;
    }

    @PutMapping
    @ApiOperation(value = "修改")
    public PPurchOrders update(@RequestBody PPurchOrders pPurchOrders) {
        pPurchOrdersDao.update(pPurchOrders);

        return pPurchOrders;
    }

    @GetMapping
    @ApiOperation(value = "列表")
    public PageTableResponse list(PageTableRequest request) {
        return new PageTableHandler(new CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return pPurchOrdersDao.count(request.getParams());
            }
        }, new ListHandler() {

            @Override
            public List<PPurchOrders> list(PageTableRequest request) {
            	List<PPurchOrders> list =  pPurchOrdersDao.list(request.getParams(), request.getOffset(), request.getLimit());
            	if(list!=null) {
            		for(PPurchOrders pPurchOrders: list) {
            			purchOrderComponent.fixPurchOrderStatusTitle(pPurchOrders);
            		}
            	}
                return list;
            }
        }).handle(request);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public void delete(@PathVariable Long id) {
        pPurchOrdersDao.delete(id);
    }
}
